package com.gjy.nwpufindseats.Fragment;

import com.gjy.nwpufindseats.MainActivity;
import com.gjy.nwpufindseats.R;
import com.gjy.nwpufindseats.util.JwNetSpider;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("CommitPrefEdits")
public class Fragment_login_info extends Fragment implements OnClickListener{
	public static SharedPreferences preferences;
	private TextView loginInfo;
	private TextView clearBtn;//清除登录信息
     @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    		Bundle savedInstanceState) {
    	View view = inflater.inflate(R.layout.fragment_login_info, container, false);
    	//页面元素初始化
    	loginInfo = (TextView) view.findViewById(R.id.logedinInfo);
    	clearBtn = (TextView) view.findViewById(R.id.clearInfo);
    	clearBtn.setOnClickListener(this); 	   	
    	preferences=getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
    	String loginInfoString = preferences.getString("xh", "0");//未登录，则返回0，登录返回学号
    	if("0".equals(loginInfoString))
    	{
    		Fragment newContent = new Fragment_login();
    		switchFragment(newContent);
    		Toast.makeText(getActivity(), "您还没有登录，请先登录", Toast.LENGTH_SHORT).show();
    		return view;
    	}
    	loginInfo.setText("已登录学号："+loginInfoString);    	
    	return view;
    }
	@Override
	public void onClick(View v) {
		dialog();
	}
	
	public void dialog() {
		  AlertDialog.Builder builder = new Builder(getActivity());
		  
		  builder.setTitle("提示");
		  builder.setMessage("确定退出该帐号并重新登录吗？");
		  //不同的监听器
		  builder.setPositiveButton("确认", new android.content.DialogInterface.OnClickListener() {
			   @Override
			   public void onClick(DialogInterface dialog, int which) {
				   Fragment newContent= null;
					Editor editor = preferences.edit();
							newContent = new Fragment_login();
							editor.clear();
							editor.commit();
							Toast.makeText(getActivity(), "已经退出，请重新登录", Toast.LENGTH_SHORT).show();					
					JwNetSpider.resetHc();//转换了登录账户，以前的静态变量hc必须重置			 
					dialog.dismiss();
					if (newContent != null)
						switchFragment(newContent);
			   } 
			   

		   });
		  builder.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() {
		   @Override
		   public void onClick(DialogInterface dialog, int which) {
		    dialog.dismiss();
		   }
		  });
		  builder.create().show();
		}
	
    private void switchFragment(Fragment fragment) {
		if (getActivity() == null)
			return;	
			MainActivity ra = (MainActivity) getActivity();
			ra.switchContent(fragment);
		
	}
}
